<?php
/**
 * Handles views and events for the UserController.
 */
class UsersView
{
	/**
	 * Shows a table listing all the users in the given array.
	 *
	 * @param array $users An array with User-objects.
	 * @return string Presents a table of users with actions like Delete, Change password.
	 */
	public function DoUserList( $users, $permissions )
	{
        $html = '';
        
        $html .= '<table>' . "\r\n";
        $html .= '    <thead>' . "\r\n";
        $html .= '        <tr>' . "\r\n";
        $html .= '            <th>UserID</th>' . "\r\n";
        $html .= '            <th>Username</th>' . "\r\n";
        $html .= '            <th>Email</th>' . "\r\n";
        $html .= '            <th>Action</th>' . "\r\n";
        $html .= '        </tr>' . "\r\n";
        $html .= '    </thead>' . "\r\n";
        
        $html .= '    <tbody>' . "\r\n";
        for( $i = 0; $i < count( $users ); $i++ )
        {
            $html .= '        <tr>' . "\r\n";
            $html .= '            <td>' . $users[$i]['id'] . '</td>' . "\r\n";
            $html .= '            <td>' . $users[$i]['name'] . '</td>' . "\r\n";
            $html .= '            <td>' . $users[$i]['email'] . '</td>' . "\r\n";
            $html .= '            <td>' . "\r\n";
            $html .= '                <form action="" method="POST">' . "\r\n";
            $html .= '                    <fieldset>' . "\r\n";
            $html .= '                        <input type="hidden" name="list_userID" value="' . $users[$i]['id'] . '" />' . "\r\n";
            $html .= '                        <input type="hidden" name="list_username" value="' . $users[$i]['name'] . '" />' . "\r\n";
            $html .= '                        <input type="submit" name="list_editOption" value="Edit" />' . "\r\n";
            $html .= '                        <input type="submit" name="list_editOption" value="Delete" />' . "\r\n";
			
			foreach ( $permissions as $permission)
			{
				$html .= '        <input disabled="disabled" type="checkbox" name="check_' . $permission->GetName() . '"  value="' . $permission->GetPermissionID() . '"';
				
				if ( in_array( $permission->GetPermissionID(), $users[$i]['permissions'] ) )
				{
					$html .= ' checked="checked"';
				}
				
				$html .= ' />' . "\r\n";
				$html .= '        <label for="check_admin">' . $permission->GetName() . '</label>' . "\r\n";
			}
			
            $html .= '                    </fieldset>' . "\r\n";
            $html .= '                </form>' . "\r\n";
            $html .= '            </td>' . "\r\n";
            $html .= '        </tr>' . "\r\n";
        }
        $html .= '    </tbody>' . "\r\n";
        
        $html .= '</table>' . "\r\n";
		
		return $html;
	}
	
	/**
	 * Shows a edit form for the user.
	 * @param User $user The userinformation to edit.
	 */
	public function DoEditUserForm( $user, $permissions, $userpermissions )
	{
		$html = '';
        
		$html .= '<form action="" method="POST">' . "\n\r";
        $html .= '    <fieldset>' . "\n\r";
		$html .= '        <input type="hidden" name="edit_userID" value="' . $user->GetUserID() . '" />' . "\r\n";
        
		$html .= '        <label for="edit_username">Username:</label>' . "\n\r";
		$html .= '        <input type="text" name="edit_username" id="edit_username" value="' . $user->GetUsername() . '" />' . "\r\n";

		$html .= '        <label for="edit_email">E-mail:</label>' . "\r\n";
		$html .= '        <input type="text" name="edit_email" id="edit_email" value="' . $user->GetEmail() . '" />' . "\r\n";
        
		$html .= '        <label for="edit_newPassword">New password:</label>' . "\r\n";
		$html .= '        <input type="password" name="edit_newPassword" id="edit_newPassword">' . "\r\n";
		
	 	$permissionCount = count( $permissions );
		
		for ($i = 0; $i < $permissionCount; $i++)
		{
			$html .= '        <input type="checkbox" name="check_' . $permissions[$i]->GetName() . '"  value="' . $permissions[$i]->GetPermissionID() . '"';
			
			if ( in_array( $permissions[$i]->GetPermissionID(), $userpermissions ) )
			{
				$html .= ' checked="checked"';
			}
			$html .= ' />' . "\r\n";
			$html .= '        <label for="check_admin">' . $permissions[$i]->GetName() . '</label>' . "\r\n";
		}
		
		$html .= '        <input type="submit" name="edit_save" value="Save" /> <input type="submit" name="edit_save" value="Cancel" />' . "\r\n";
        $html .= '    </fieldset>' . "\n\r";
		$html .= '</form>' . "\r\n";
		
		return $html;
	}
	
	public function CreateUserButton()
	{
		$html = '';
		
		$html .= '<form action="" method="post">' . "\r\n";
		$html .= '    <fieldset>' . "\r\n";
		
		$html .= '        <input type="submit" name="CreateUser" id="CreateUser" value="Create user" />' . "\r\n";
	
		$html .= '    </fieldset>' . "\r\n";
		$html .= '</form>' . "\r\n";
		
		return $html;
	}
	
	public function DoRegisterForm( $permissions )
	{
		$html = '';
		
		$html .= '<form action="" method="post">' . "\r\n";
		$html .= '    <fieldset>' . "\r\n";
		
		$html .= '        <label for="create_username">Username:</label>' . "\r\n";
		$html .= '        <input type="text" name="create_username" id="create_username" required />' . "\r\n";
		
		$html .= '        <label for="create_password">Password:</label>' . "\r\n";
		$html .= '        <input type="password" name="create_password" id="create_password" required />' . "\r\n";
		
		$html .= '        <label for="create_email">Email:</label>' . "\r\n";
		$html .= '        <input type="text" name="create_email" id="create_email" required />' . "\r\n";
	
		foreach ( $permissions as $permission)
		{
		
			$html .= '        <input type="checkbox" name="check_' . $permission->GetName() . '"  value="' . $permission->GetPermissionID() . '"/>' . "\r\n";
			$html .= '        <label for="check_admin">' . $permission->GetName() . '</label>' . "\r\n";
		}
		
		$html .= '        <input type="submit" name="create_save" id="create_save" value="Save" />' . "\r\n";
	
		$html .= '    </fieldset>' . "\r\n";
		$html .= '</form>' . "\r\n";
		
		return $html;
	}
	
	
	## START: GET USER CREATE POST DATA ##
	public function CreateUser()
	{
		return isset( $_POST['CreateUser'] );
	}
	
	public function GetCreateUsername()
	{
		return isset( $_POST['create_username'] ) ? $_POST['create_username'] : NULL;
	}
	
	public function GetCreatePassword()
	{
		return isset( $_POST['create_password'] ) ? $_POST['create_password'] : NULL;
	}
	
	public function GetCreateEmail()
	{
		return isset( $_POST['create_email'] ) ? $_POST['create_email'] : NULL;
	}
	
	public function CreateSave()
	{
		return isset( $_POST['create_save'] );
	}
	
	## END: GET USER CREATE POST DATA ##
		
	## START: GET USER LIST POST DATA ##

	/**
	 * Checks if the POST DeleteUser is set.
	 * @return bool True/False depending on if the user clicked "Delete User" in the user list view.
	 */
	public function DeleteUser()
	{
        return isset( $_POST['list_editOption'] ) && $_POST['list_editOption'] === "Delete";
	}
	
	/**
	 * Checks if the POST EditUser is set.
	 * @return bool True/False depending on if the user clicked "Edit User" in the user list view.
	 */
	public function EditUser()
	{
        return isset( $_POST['list_editOption'] ) && $_POST['list_editOption'] === "Edit";
	}

    /**
	 * Gets the UserID from POST.
	 * @return int Returns the UserID for the user, from the user list, to edit.
	 */
    public function GetListUserID()
    {
        return ( isset( $_POST['list_userID'] ) ) ? $_POST['list_userID'] : NULL;
    }
    
    /**
	 * Gets the Username from POST.
	 * @return string Returns the Username for the user, from the user list, to edit.
	 */
    public function GetListUsername()
    {
        return ( isset( $_POST['list_username'] ) ) ? $_POST['list_username'] : NULL;
    }
    
    ## END: GET USER LIST POST DATA ##
    
    
    ## START: GET EDIT USER DATA ##
    
	/**
	 * Checks if the user clicked on Cancel.
	 * @return bool True/False depending on if the user clicked "Cancel" in the user edit view.
	 */
    public function EditCancel()
    {
        return isset( $_POST['edit_save'] ) && $_POST['edit_save'] === "Cancel";
    }
	 
	/**
	 * Checks if the user clicked on Save.
	 * @return bool True/False depending on if the user clicked "Save" in the user edit view.
	 */
    public function EditSave()
    {
        return isset( $_POST['edit_save'] ) && $_POST['edit_save'] === "Save";
    }
    
    /**
	 * Gets the UserID from POST.
	 * @return string Returns the UserID for the user in the edit view.
	 */
    public function GetEditUserID()
    {
        return ( isset( $_POST['edit_userID'] ) ) ? $_POST['edit_userID'] : NULL;
    }
    
    /**
	 * Gets the Username from POST.
	 * @return string Returns the Username for the user in the edit view.
	 */
    public function GetEditUsername()
    {
        return ( isset( $_POST['edit_username'] ) ) ? $_POST['edit_username'] : NULL;
    }
    
    /**
	 * Gets the Email from POST.
	 * @return string Returns the Email for the user in the edit view.
	 */
    public function GetEditEmail()
    {
        return ( isset( $_POST['edit_email'] ) ) ? $_POST['edit_email'] : NULL;
    }
    
    /**
	 * Gets the New Password from POST.
	 * @return string Returns the New Password for the user in the edit view.
	 */
    public function GetEditNewPassword()
    {
        return ( isset( $_POST['edit_newPassword'] ) ) ? $_POST['edit_newPassword'] : NULL;
    }
    
    ## END: GET EDIT USER DATA ##
}
